<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.7.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="Android中的测试手段非常丰富。通常上，按测试运行环境来划分，可以分为本地单元测试与Instrument测试：">
<meta name="keywords" content="android">
<meta property="og:type" content="article">
<meta property="og:title" content="Android SDK中的自动化测试">
<meta property="og:url" content="https://ricky9090.github.io/2016/08/11/android_automation_test/index.html">
<meta property="og:site_name" content="Ricky&#39;s Blog">
<meta property="og:description" content="Android中的测试手段非常丰富。通常上，按测试运行环境来划分，可以分为本地单元测试与Instrument测试：">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-06-11T08:46:43.905Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android SDK中的自动化测试">
<meta name="twitter:description" content="Android中的测试手段非常丰富。通常上，按测试运行环境来划分，可以分为本地单元测试与Instrument测试：">
  <link rel="canonical" href="https://ricky9090.github.io/2016/08/11/android_automation_test/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>Android SDK中的自动化测试 | Ricky's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Ricky's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">A long way to go.</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-about">
      
    

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://ricky9090.github.io/2016/08/11/android_automation_test/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Ricky">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Ricky's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">Android SDK中的自动化测试

          
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2016-08-11 18:14:11" itemprop="dateCreated datePublished" datetime="2016-08-11T18:14:11+08:00">2016-08-11</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2018-06-11 16:46:43" itemprop="dateModified" datetime="2018-06-11T16:46:43+08:00">2018-06-11</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>Android中的测试手段非常丰富。通常上，按测试运行环境来划分，可以分为本地单元测试与Instrument测试：<br><a id="more"></a><br>|运行环境|测试类型|<br>|—|—|<br>|PC机|本地Unit Test|<br>|Android手机或模拟器|Instrument Test|<br><br><br>这两种类型测试源码在工程的位置分别为：</p>
<table>
<thead>
<tr>
<th>测试类型</th>
<th>对应目录</th>
</tr>
</thead>
<tbody>
<tr>
<td>本地Unit Test</td>
<td>module-name/src/test/java</td>
</tr>
<tr>
<td>Instrument Test</td>
<td>module-name/src/androidTest/java</td>
</tr>
</tbody>
</table>
<p><br><br>这两种测试使用的Runner不同：</p>
<table>
<thead>
<tr>
<th>测试类型</th>
<th>TestRunner</th>
</tr>
</thead>
<tbody>
<tr>
<td>本地Unit Test</td>
<td>JUnit4</td>
</tr>
<tr>
<td>Instrument Test</td>
<td>JUnit3</td>
</tr>
<tr>
<td></td>
<td>AndroidJunit4</td>
</tr>
</tbody>
</table>
<p><br><br>本地Unit Test由于使用Junit4，因此与java中的单元测试区别不大，可以灵活的使用Mockito来模拟调用Android的API。同时Android Studio在构建APP时，也会自动生成一个MockableAndroid.jar来支持这一需求。<br>Instrument类型测试由于类库较多，因此使用的Runner也有所不同。比较常见到的有：</p>
<table>
<thead>
<tr>
<th>类名</th>
<th>说明</th>
<th>Runner</th>
</tr>
</thead>
<tbody>
<tr>
<td>ApplicationTestCase</td>
<td>Studio默认生成的类，用于测试Application。其可以完整控制Application的生命周期，并且可以通过setContext传入Mock的Context对象进行注入测试。</td>
<td>Junit3</td>
</tr>
<tr>
<td>InstrumentationTestCase</td>
<td>自Eclipse环境起常用的测试类。可以通过获取到Instrument控制APP运行，内部方法较为通用，通常需要二次封装，并且APP中改进一些测试友好的设计。</td>
<td>Junit3</td>
</tr>
<tr>
<td>ActivityInstrumentationTestCase<t></t></td>
<td>专门设计来测试Activity的类，不过现在已经废弃了，由ActivityInstrumentationTestCase2<t>代替</t></td>
<td>AndroidJunit4</td>
</tr>
<tr>
<td>ActivityInstrumentationTestCase2<t></t></td>
<td>新的测试Activity的类，可以通过getAvtivity方法直接获取到被测Activity；不过查阅最新的api24文档，这个类也可能要被废弃</td>
<td>AndroidJunit4</td>
</tr>
<tr>
<td>ServiceTestCase<t></t></td>
<td>专门测试Service的类</td>
<td>AndroidJunit4</td>
</tr>
</tbody>
</table>
<p><br><br>以上几个类在Android SDK中就可以找到，都位于<code>android.test</code>包中。当然这个包中还有很多其他的测试类。<br>在Android 5.0版本起，Google又提供了一个专门用于自动化测试的Support包<code>Android Testing Support Library</code>,其中包括了测试所必要的<code>AndroidJUnitRunner</code>，还包括了两个重要组件：<br><code>Espresso</code>与<code>UI Automator</code>,这两者均是用来做UI自动化测试的框架。区别在于<code>Espresso</code>适用于单APP内部UI测试，而<code>UI Automator</code>适用于跨APP测UI测试。</p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/android/" rel="tag"># android</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2016/08/11/ios_unit_test/" rel="next" title="iOS中异步回调的单元测试方法">
                  <i class="fa fa-chevron-left"></i> iOS中异步回调的单元测试方法
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2016/08/23/android_mock_gps/" rel="prev" title="Android系统中模拟GPS位置">
                  Android系统中模拟GPS位置 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Ricky</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/ricky9090" title="GitHub &rarr; https://github.com/ricky9090" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ricky</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.7.1</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.4.0</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  





















  

  

  

</body>
</html>
